package com.kamistoat.meimeimall.common.redisTemplates;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * 公共依赖。
 * 向容器中注入各个redis集群的Template
 */
@Configuration
public class InjectRedisTemplate {
    @Bean("redisOrderLogTemplate")
    public StringRedisTemplate injectRedisOrderLogTemplate() {
        StringRedisTemplate orderLogTemplate = new StringRedisTemplate();
        RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration();
        List<RedisNode> nodes = new ArrayList<>();
        nodes.add(new RedisNode("172.19.0.20", 6379));
        nodes.add(new RedisNode("172.19.0.21", 6479));
        clusterConfiguration.setClusterNodes(nodes);
        orderLogTemplate.setConnectionFactory(new LettuceConnectionFactory(clusterConfiguration));
        return orderLogTemplate;
    }

    @Bean("redisProductTemplate")
    public StringRedisTemplate injectProductTemplate() {
        StringRedisTemplate productTemplate = new StringRedisTemplate();
        RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration();
        List<RedisNode> nodes = new ArrayList<>();
        nodes.add(new RedisNode("172.19.0.30", 6379));
        nodes.add(new RedisNode("172.19.0.31", 6379));
        clusterConfiguration.setClusterNodes(nodes);
        productTemplate.setConnectionFactory(new LettuceConnectionFactory(clusterConfiguration));
        return productTemplate;
    }
}
